<template>
    <div>
        <!-- 顶部滑动条 -->
        <div id="slider" class="mui-slider">
            <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <div class="mui-scroll">
                    <a :class="['mui-control-item', item.id == 0?'mui-active':'' ]" v-for="item in cates" :key="item.id" @click="getPhotoListByCateId(item.id)">
                        {{ item.title }}
                    </a>
                </div>
            </div>
        </div>
        <!-- 图片加载 -->
        <ul class="photo-list">
            <router-link v-for="item in images" :key="item.id" tag="li" :to="'/home/photoinfo/' + item.id">
                <img v-lazy="item.img_url">
                <div class="info">
                    <h1 class="info-title">{{item.title}}</h1>
                    <div class="info-body">{{item.zhaiyao}}</div>
                </div>
            </router-link>
        </ul>
    </div>
</template>

<script>
import {Toast} from 'mint-ui'
// 1. 导入 mui 的 js 文件
import mui from '../../lib/mui/js/mui.js'
export default {
    data(){
        return {
            cates:[],
            images:[]
        }
    },
    created(){
        this.getAllCategroy()
        this.getPhotoListByCateId(0)
    },
    mounted(){
        // 当组件中的 DOM 元素被渲染好并挂载到页面中之后会执行这个钩子函数
        // 如果要操作页面中的元素，最好在 mounted 里面，因为这个时候的 dom 元素是最新的
        // 2. 初始化滑动控件
        mui('.mui-scroll-wrapper').scroll({
            deceleration: 0.0005
        })
    },
    methods:{
        getAllCategroy(){
            this.$http.get('api/getimgcategory').then(result=>{
                if(result.body.status === 0){
                    result.body.message.unshift({ id:0 , title:'全部' })
                    this.cates = result.body.message
                }else{
                    Toast("获取失败!")
                }
            })
        },
        getPhotoListByCateId(id){
            this.$http.get('api/getimages/' +id).then(result=>{
                if(result.body.status === 0){
                    console.log(result.body.message)
                    this.images = result.body.message
                }else{
                    Toast("获取失败!")
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
*{
    touch-action:pan-y; // 取消滑动时候的警告，这个警告和是否能够滑动无关
    margin:0;
    padding:0;
}
.photo-list{
    list-style:none;
    padding:10px;
    padding-bottom:0;
    li{
        // background-color:#ccc;
        text-align:center;
        margin-bottom:10px;
        position:relative;
        img{
            width:100%;
            border-radius:15px;
            vertical-align:middle;
            box-shadow:0 0 9px #737272;
        }
        img[lazy=loading] {
            width: 300px;
            height: 300px;
            margin: auto;
        }
        .info{
            background-color:black;
            opacity: .7;
            color:#fff;
            position:absolute;
            bottom:0;
            border-bottom-left-radius: 15px;
            border-bottom-right-radius: 15px;
            max-height:100px;
            .info-title{
                font-size:13px;
                height:30px;
                line-height:30px;
                white-space:nowrap;
                text-align:left;
                text-overflow:ellipsis;
            }
            .info-body{
                font-size:12px;
                text-align:left;
                padding-left:4px;
            }
        }
    }
}
</style>